package com.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mysql.jdbc.Statement;
import com.web.dao.OrderDAO;
import com.web.model.Order;
import com.web.model.User;
import com.web.util.DBConnection;

public class OrderDAOImp implements OrderDAO{
	
	private static final String INSERT="insert into d_order"
			+"(id,user_id,status,order_time,order_desc,total_price,receive_name,full_address,postal_code,mobile,phone)"
			+"values(?,?,?,?,?,?,?,?,?,?,?)";
	
	public void save(Order order) throws Exception{
		PreparedStatement prep= null;
		Connection conn= null;
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(INSERT,Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1,order.getId());
			prep.setInt(2, order.getUserId());
			prep.setInt(3, order.getStatus());
			prep.setLong(4, order.getOrderTime());
			prep.setString(5, order.getOrderDesc());
			prep.setDouble(6, order.getTotalPrice());
			prep.setString(7, order.getReceiveName());
			prep.setString(8,order.getFullAddress());
			prep.setString(9, order.getPostalCode());
			prep.setString(10,order.getMobile());
			prep.setString(11, order.getPhone());
			prep.executeUpdate();
			ResultSet rst= prep.getGeneratedKeys();
			rst.next();
			int id= rst.getInt(1);
			order.setId(id);
		}finally{
			DBConnection.close(prep, conn);
		} 
	}
}

